package work.lrl.coupon_send;

import cn.hutool.json.JSONObject;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import work.MemberUtil;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class Setup4 {

    public static void main(String[] args) {
        EasyExcel.read(Setting.setup3, Demo.ExcelObject.class, new Setup4Lis()).sheet().doRead();
    }


    public static class Setup4Lis implements ReadListener<Demo.ExcelObject> {
        List<Demo.ExcelObject> list = new CopyOnWriteArrayList<>();
        AtomicInteger count = new AtomicInteger();
        NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(16);
        volatile String token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJtYW5hZ2VyIiwiY2xpZW50VHlwZSI6InVzZXIiLCJ0ZW5hbnRJZCI6ImxybF9sb3JlYWwiLCJleHAiOjE2OTUzNjk3MjEsImp0aSI6IlNVdEhyaEJGamNFc29Oa3VCRGFQS2QifQ.ibY6s8GL_rtWU62WLTtsxbrmongo2NwEfc0ZX7YZ1dQf_MS_DN5XBeiUMHHHlGl9xDMO0c2QEQ1nPpUrtvtgHA";


        @Override
        public void invoke(Demo.ExcelObject excelObject, AnalysisContext analysisContext) {
            nioEventLoopGroup.execute(() -> {
                if (count.getAndIncrement() % 2000 == 0) {
                    log.info("【卡券群发】发放 进度 ->{}", count.get());
                }
                list.add(excelObject);
                if ("否".equals(excelObject.getHit())) {
                    excelObject.setSuccess("否");
                    return;
                }
                try {
                    sendCoupon(excelObject);
                } catch (Exception e) {
                    e.printStackTrace();
                    excelObject.setSuccess("否");
                }
            });
        }

        private void sendCoupon(Demo.ExcelObject excelObject) {
            JSONObject sendRes = MemberUtil.couponSend(excelObject.getCustomerId(), Setting.templateId, Setting.baseUrl, token);
            if (Objects.isNull(sendRes) || (!sendRes.containsKey("code")) || (!sendRes.getStr("code").equals("0"))) {
                log.error("【发放卡券】结果-》{}", sendRes);
//                Scanner sc = new Scanner(System.in);
//                System.out.println("请判断是否是token失效，是请输入1");
//                String isTokenInvalid = sc.next();
//                if ("1".equals(isTokenInvalid)) {
//                    token = sc.next();
//                    sendCoupon(excelObject);
//                    return;
//                }
                excelObject.setErrMsg(sendRes.toString());
                excelObject.setSuccess("否");

            } else {
                excelObject.setSuccess("是");
            }
        }

        @SneakyThrows
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            nioEventLoopGroup.shutdownGracefully().sync();
            EasyExcel.write(Setting.setup4, Demo.ExcelObject.class).sheet("sheet1").doWrite(list);
        }

    }

}
